package com.wushi.lenovo.asia5b.app_ui.profile.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mvp.base.BaseMvpActivity;
import com.mvp.base.model.ResultModel;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.profile.mvpview.ChangeAddressActivityView;
import com.wushi.lenovo.asia5b.app_di.profile.presenter.ChangeAddressActivityPresenter;
import com.wushi.lenovo.asia5b.app_event.ChangeAddressEvent;
import com.wushi.lenovo.asia5b.app_event.NewAddressEvent;
import com.wushi.lenovo.asia5b.app_model.profile.AddressModel;
import com.wushi.lenovo.asia5b.app_model.result.AddressResModel;
import com.wushi.lenovo.asia5b.app_ui.profile.adapter.ChangeAddressAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.functions.Function;

/**
 * Created by zhangyuncai on 2019/1/25.
 */
public class ChangeAddressActivity extends BaseMvpActivity<
        ChangeAddressActivityView, ChangeAddressActivityPresenter> implements ChangeAddressActivityView {
    @BindView(R.id.recyclerview_shipaddress)
    RecyclerView mRecyclerView;
    private final List<AddressModel> mDatas = new ArrayList<>();
    private ChangeAddressAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_changeaddress;
    }

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        initView();

        initData();

        initEvent();
    }

    @Override
    protected void initView() {
        mAdapter = new ChangeAddressAdapter(mDatas);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        getAddresses();
    }

    private void getAddresses() {
        getPresenter().getAddresses();
    }

    @Override
    protected void initEvent() {
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                AddressModel model = mAdapter.getItem(position);
                if (view.getId() == R.id.tv_ship_apply) {
                    EventBus.getDefault().post(new ChangeAddressEvent(model));
                    finish();
                }
            }
        });
    }

    @Override
    @OnClick({
            R.id.bt_ship_newaddres
    })
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_ship_newaddres:
                startActivity(new Intent(this, NewAddressActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    public void getAddressesResult(ResultModel<AddressResModel> addressResModelResultModel) {
        Observable.just(addressResModelResultModel)
                .map(new Function<ResultModel<AddressResModel>, List<AddressModel>>() {
                    @Override
                    public List<AddressModel> apply(ResultModel<AddressResModel> addressResModelResultModel) throws Exception {
                        return addressResModelResultModel.getData().getAddresses();
                    }
                })
                .subscribe(addressModels -> {
                    if (addressModels != null) {
                        mDatas.clear();
                        mDatas.addAll(addressModels);
                        mAdapter.notifyDataSetChanged();
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                });
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onChangeAddresses(NewAddressEvent event) {
        if (event != null) {
            getAddresses();
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
